home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / dashed lines / dashed lines.p < prev    next >
Encoding:
Text File  |  2000-09-28  |  5.8 KB  |  216 lines

  1. {
  2.     File:        Dashed Lines.p
  3.  
  4.     Contains:    Dashed Lines demonstrates how to draw dashed line objects on PostScript printers.
  5.                 This simple example does not deal with QuickDraw printers, on which it draws solid
  6.                 black lines.
  7.  
  8.     Written by: Dave Hersey    
  9.  
  10.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/23/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. }
  25.  
  26. PROGRAM Dashed_Lines;
  27.  
  28. USES
  29.     QuickDraw,Memory,Printing,Fonts,Events;
  30.  
  31.  
  32. CONST
  33.     DashedLine            = 180;    (*    Begin PostScript line dashing            *)
  34.     DashedStop            = 181;    (*    End PostScript line dashing                *)
  35.     SetLineWidth        = 182;    (*    Set hi resolution line width             *)    
  36.  
  37. TYPE
  38.     TDashedLineHdl = ^TDashedLinePtr;
  39.     TDashedLinePtr = ^TDashedLine;
  40.     TDashedLine = PACKED RECORD
  41.         offset:        SignedByte;
  42.         centered:    SignedByte;
  43.         dashed:        ARRAY [0..1] OF SignedByte;
  44.     END;
  45.  
  46.  
  47.   {*------ DrawStuff -----------------------------------------------------------------*}
  48.  
  49. {**
  50.  **      DrawStuff draws QuickDraw objects with dashed lines in Postscript.
  51.  **}
  52.  
  53.  PROCEDURE DrawStuff (theWorld : Rect; theGPort : GrafPtr);
  54.  
  55. TYPE
  56.     widhdl = ^widptr;
  57.     widptr = ^widpt;
  58.     widpt = Point;
  59.  
  60.  VAR
  61.    oldPort      :     GrafPtr;
  62.    arect        :    Rect;
  63.    Width        :    Widhdl;
  64.    dashedln     :    TDashedLineHdl;
  65.    myPic        :    PicHandle;
  66.  
  67.  
  68. BEGIN
  69.     GetPort (oldPort);
  70.  
  71.     SetPort (theGPort);
  72.     Dashedln := TDashedLineHdl(NewHandle(sizeof(tdashedline)));
  73.     Dashedln^^.offset := 0;       {No offset}
  74.     Dashedln^^.centered := 0;     {don’t center}
  75.     Dashedln^^.dashed[0] := 1;    {this is the length }
  76.     Dashedln^^.dashed[1] := 4;    {this means 4 points on, 4 points off }
  77.     
  78.     Width := widhdl(NewHandle(sizeof(widpt)));
  79.     Width^^.h := 4;                {denominator is 4}
  80.     Width^^.v := 1;               {numerator is 1}
  81.     
  82.     myPic := OpenPicture(theWorld);
  83.     PenSize(1,1);                {Set the pen size to 1 wide x 1 high }
  84.     ClipRect(theWorld);
  85.  
  86.     PicComment(DashedLine,GetHandleSize(Handle(dashedln)),Handle(dashedln)); 
  87.     PicComment(SetLineWidth,4,Handle(width));   {SetLineWidth to 1 pixel wide.}
  88.  
  89.     SetRect(arect,100,100,500,500);    {Draw some stuff in dash mode.}
  90.     FrameRect(aRect);
  91.     MoveTo(500,500);
  92.     Lineto(100,100);
  93.     MoveTo(100,500);
  94.     Lineto(500,100);
  95.     InsetRect(arect,10,10);
  96.     FrameOval(aRect);
  97.     InsetRect(arect,10,10);
  98.     FrameOval(aRect);
  99.     InsetRect(arect,10,10);
  100.     FrameOval(aRect);
  101.     InsetRect(arect,10,10);
  102.     FrameOval(aRect);
  103.     InsetRect(arect,10,10);
  104.     FrameOval(aRect);
  105.     InsetRect(arect,10,10);
  106.     FrameOval(aRect);
  107.     InsetRect(arect,10,10);
  108.     FrameOval(aRect);
  109.     InsetRect(arect,10,10);
  110.     FrameOval(aRect);
  111.     InsetRect(arect,10,10);
  112.     FrameOval(aRect);
  113.     PicComment(DashedStop,0,nil);    {DashedStop}
  114.  
  115.   ClosePicture;
  116.   DisposeHandle(handle(width));           {Clean up}
  117.   DisposeHandle(handle(dashedln));
  118.   DrawPicture(MyPic, theWorld);          {print it}
  119.   KillPicture(MyPic);    SetPort(oldPort);
  120.  
  121.  END;  {**  DrawStuff  **}
  122.  
  123.  
  124. {*------ PrintStuff ----------------------------------------------------------------*}
  125. {**
  126.  **        PrintStuff will call all of the nescessary Print Manager calls to print 
  127.  **        a document. It checks PrError() after each Print Manager call. If an error 
  128.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  129.  **        will have a corresponding close call before the error is posted to the user. 
  130.  **        You want to use this approach to make sure the Print Manager closes properly 
  131.  **        and all temporary memory is released.
  132.  **}
  133.  
  134. PROCEDURE PrintStuff;
  135.  
  136. VAR
  137.   oldPort              : GrafPtr;
  138.   thePrRecHdl        : THPrint;
  139.   thePrPort            : TPPrPort;
  140.   theStatus            : TPrStatus;
  141.     
  142. BEGIN
  143.    GetPort(oldPort);
  144.     
  145.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  146.     
  147.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  148.     BEGIN
  149.        PrOpen;
  150.        IF (PrError = noErr) THEN
  151.         BEGIN
  152.            PrintDefault(thePrRecHdl);
  153.  
  154.            IF (PrError = noErr) THEN
  155.             BEGIN
  156.                IF (PrStlDialog(thePrRecHdl)) THEN
  157.                 BEGIN
  158.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  159.                     BEGIN
  160.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  161.                                
  162.                       IF (PrError = noErr) THEN
  163.                         BEGIN
  164.  
  165.                              PrOpenPage(thePrPort, NIL);
  166.                                 
  167.                           IF (PrError = noErr) THEN
  168.                             BEGIN
  169.                               {**
  170.                                   rPage (IM II-150) is the printable area for the  
  171.                                   currently selected printer. By passing the current  
  172.                                   port to the draw routine, enables your app
  173.                                   to use the same routine to draw to the screen
  174.                                   and the printer's GrafPort.
  175.                                **}
  176.                                     
  177.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  178.                                            GrafPtr (thePrPort));
  179.                                  
  180.                              END;
  181.                             PrClosePage(thePrPort);
  182.                           END;
  183.                              
  184.                           PrCloseDoc(thePrPort);
  185.                              
  186.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  187.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, @theStatus);
  188.  
  189.                       END;
  190.                   END;
  191.               END;
  192.           END;
  193.         
  194.         PrClose;
  195.  
  196.      END;
  197.  
  198. END;  {**  PrintStuff  **}
  199.  
  200.  
  201. {*------ main ----------------------------------------------------------------------*}
  202.  
  203. BEGIN
  204.     
  205.     InitGraf(@qd.thePort);
  206.     InitFonts;
  207.     FlushEvents(everyEvent, 0);    
  208.     InitWindows;
  209.     InitMenus;
  210.     TEInit;
  211.     InitDialogs(NIL);
  212.     InitCursor;
  213.  
  214.     PrintStuff;
  215.  
  216. END. {**  main  **}